<template>
  <div class="login-wrapper">
    <div class="login-form">
      <h3 class="title">微型问卷系统后台管理</h3>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="top"
        class="form-area"
        @submit.native.prevent
        @keyup.enter="handleSubmit('form')"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" autocomplete="on" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            native-type="submit"
            @click="handleSubmit('form')"
          >立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="login-logo" />
    <div class="login-github">
      <a class="icon-github" href="https://github.com/52admln/vue-questionnaire" target="_blank">
        <img
          src=""
          alt="GitHub"
        ></a>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { Form as ElForm } from 'element-ui'

import { UserModule } from '@/store/modules/user'
import * as AdminAction from '@/api/admin'

@Component
export default class LoginComponent extends Vue {
  private form = {
    username: '',
    password: ''
  }
  private rules = {
    username: [
      {
        required: true,
        message: '请输入用户名',
        trigger: 'blur'
      }
    ],
    password: [
      {
        required: true,
        message: '请输入密码',
        trigger: 'blur'
      }
    ]
  }

  handleSubmit (name: string) {
    (this.$refs[name] as ElForm).validate(async (valid) => {
      if (valid) {
        const res = await AdminAction.login({
          ...this.form
        })
        if (res.success) {
          this.$message.success('登录成功!')
          const data: any = {
            username: this.form.username,
            token: res.data
          }
          UserModule.login(data)
          await this.$router.push('/list')
        } else {
          this.$message.error('登录失败，请检查账号密码是否正确。')
        }
      } else {
        this.$message.error('表单填写有误!')
      }
    })
  }
}
</script>

<style lang="scss" scoped>
  .login-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #318fce;
    background: linear-gradient(to bottom right, #318fce 0%, #48e7ef 100%);

    .login-form {
      background-color: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, .2);
      border-radius: 5px;
      padding: 30px 20px;
    }

    .title {
      font-size: 18px;
      font-weight: 700;
      padding-bottom: 10px;
    }

    .login-btn {
      width: 100%;
    }

    .form-area {
      width: 300px;
    }

    .login-logo {
      position: absolute;
      top: 10px;
      left: 30px;
      width: 250px;
      height: 75px;
      background: url("../../assets/logo.png") no-repeat center;
      background-size: cover;
    }

    .login-github {
      position: absolute;
      top: 20px;
      right: 30px;
      margin: 20px 0;
      color: #fff;
      vertical-align: middle;
    }

    .icon-github {
      display: inline-block;
    }

    .icon-github img{
      width: 20px;
      height: auto;
    }
  }
</style>
